package com.zhangmeng.online.exam.ui.api.model;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.zhangmeng.online.exam.ui.admin.PaperPage;
import com.zhangmeng.online.exam.ui.api.DataLoad;
import com.zhangmeng.online.exam.ui.components.ExamComponent;
import com.zhangmeng.online.exam.ui.components.PaperViewComponent;
import com.zhangmeng.online.exam.ui.components.callBack.PaperViewComponentCallBackImpl;
import com.zhangmeng.online.exam.ui.utils.AlertUtils;
import com.zhangmeng.online.exam.ui.utils.ApiUtils;
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.ObservableList;
import javafx.scene.Parent;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.util.*;

/**
 * @author zm
 * @date 2025/3/20 16:31
 * @version: 1.0
 */
public class ExamDataLoad implements DataLoad {

    private Parent form;

    @Override
    public ApiUtils.DataView loadData(Integer pageNum, Integer pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("pageNum", pageNum.toString());
        params.put("pageSize", pageSize.toString());
        String userListData = HttpUtils.GET(ApiUtils.API_URL + "/exam/list", params);
        JSONObject jsonObject = JSON.parseObject(userListData);
        JSONArray data = jsonObject.getJSONArray("data");
        int total = jsonObject.getIntValue("total");
        List<Map<String, SimpleStringProperty>> userMapList = new ArrayList<>();
        for (Object datum : data) {
            JSONObject user = (JSONObject) datum;
            Map<String, SimpleStringProperty> userMap = new HashMap<>();
            userMap.put("序号", new SimpleStringProperty(user.getString("id")));
            userMap.put("paperId", new SimpleStringProperty(user.getString("paperId")));
            userMap.put("试卷名称", new SimpleStringProperty(user.getString("paperName")));
            userMap.put("考试时间", new SimpleStringProperty(user.getString("examTime")));
            userMap.put("科目名称", new SimpleStringProperty(user.getString("subjectName")));
            userMap.put("总分", new SimpleStringProperty(user.getString("score")));
            userMap.put("状态", new SimpleStringProperty(user.getString("status")));

            userMap.put("id", new SimpleStringProperty(user.getString("id")));
            userMapList.add(userMap);
        }

        ApiUtils.DataView dataView = new ApiUtils.DataView(Arrays.asList("序号", "试卷名称", "考试时间", "科目名称", "总分", "状态"), userMapList).setDataLoad(this).setPageNum(pageNum).setPageSize(pageSize).setTotal(total);
        dataView.setDataLoad(this);
        return dataView;
    }

    @Override
    public void setForm(Parent view) {
        this.form = view;
    }

    @Override
    public Parent getForm() {
        return this.form;
    }

    @Override
    public Map<String, Object> saveForm(Map<String, Object> map) {
        return null;
    }

    @Override
    public void deleteData(String id) {

    }

    @Override
    public void editData(String s, Stage stage) {

    }

    @Override
    public void updateData(String id, String name, String oldValue, String newValue) {

    }

    @Override
    public List<MenuItem> getContextMenu() {
        //考试
        MenuItem  exam_menu = exam_menu();
        return Arrays.asList(exam_menu);
    }


    private MenuItem exam_menu(){
        MenuItem group_volumes = new MenuItem("开始考试");
        group_volumes.setOnAction(event -> {
            Map<String, SimpleStringProperty> map = ApiUtils.selectedItem(group_volumes);
            String id = map.get("id").getValue();
            String paperId = map.get("paperId").getValue();
//            AlertUtils.alert_msg("组卷正在开发中..." + id);
            ContextMenu parentPopup = group_volumes.getParentPopup();
            BorderPane indexPage = (BorderPane) parentPopup.getStyleableParent();
            Stage stage = (Stage) indexPage.getScene().getWindow();

            Map<String, Object> context = new HashMap<>();
            context.put("id", id);
            context.put("paperId", paperId);

            PaperPage paperViewComponent = new PaperPage(context);

            AlertUtils.alert("组卷", paperViewComponent, 1280, 720, stage, true);

        });
        return group_volumes;
    }

    @Override
    public ApiUtils.DataView searchData(Map<String, Object> searchText, int pageNum, int pageSize) {
        return null;
    }

    public  ApiUtils.DataView loadPaperData(int pageNum, int pageSize){

        Map<String, Object> params = new HashMap<>();
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);

        String userListData = HttpUtils.GET(ApiUtils.API_URL + "/exam/user/list", params);
        JSONObject jsonObject = JSON.parseObject(userListData);
        JSONArray data = jsonObject.getJSONArray("data");
        int total = jsonObject.getIntValue("total");
        List<Map<String, SimpleStringProperty>> userMapList = new ArrayList<>();

        for (Object datum : data) {
            JSONObject user = (JSONObject) datum;
            Map<String, SimpleStringProperty> userMap = new HashMap<>();
            userMap.put("序号", new SimpleStringProperty(user.getString("id")));
            userMap.put("用户名", new SimpleStringProperty(user.getString("username")));
            userMap.put("手机号码", new SimpleStringProperty(user.getString("phone")));
            userMap.put("电子邮箱", new SimpleStringProperty(user.getString("email")));
            userMap.put("角色", new SimpleStringProperty(user.getString("type")));
            userMap.put("id", new SimpleStringProperty(user.getString("id")));
            userMapList.add(userMap);
        }

        ApiUtils.DataView dataView = new ApiUtils.DataView(Arrays.asList("序号", "用户名", "手机号码", "电子邮箱", "角色"), userMapList).setDataLoad(this).setPageNum(pageNum).setPageSize(pageSize).setTotal(total);
        dataView.setDataLoad(this);

        return dataView;
    }

    public ApiUtils.DataView loadPaperData(Map<String, Object> context, int pageNum, int pageSize){

        Map<String, Object> params = new HashMap<>();
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        params.put("paperId", context.get("id"));
        String userListData = HttpUtils.GET(ApiUtils.API_URL + "/exam/user/list", params);
        JSONObject jsonObject = JSON.parseObject(userListData);
        JSONArray data = jsonObject.getJSONArray("data");
        int total = jsonObject.getIntValue("total");
        List<Map<String, SimpleStringProperty>> userMapList = new ArrayList<>();

        for (Object datum : data) {
            JSONObject user = (JSONObject) datum;
            Map<String, SimpleStringProperty> userMap = new HashMap<>();
            userMap.put("序号", new SimpleStringProperty(user.getString("id")));
            userMap.put("用户名", new SimpleStringProperty(user.getString("username")));
            userMap.put("手机号码", new SimpleStringProperty(user.getString("phone")));
            userMap.put("电子邮箱", new SimpleStringProperty(user.getString("email")));
            userMap.put("角色", new SimpleStringProperty(user.getString("type")));
            userMap.put("id", new SimpleStringProperty(user.getString("id")));
            userMapList.add(userMap);
        }

        ApiUtils.DataView dataView = new ApiUtils.DataView(Arrays.asList("序号", "用户名", "手机号码", "电子邮箱", "角色"), userMapList).setDataLoad(this).setPageNum(pageNum).setPageSize(pageSize).setTotal(total);
        dataView.setDataLoad(this);

        return dataView;
    }

    public ApiUtils.DataView searchPaperData(String searchText, int pageNum, int pageSize){
        return null;
    }

    public  ApiUtils.DataView loadChoosePaperData(Map<String, Object> context, Integer pageNum, Integer pageSize){

        Map<String, Object> params = new HashMap<>();
        params.put("pageNum", pageNum.toString());
        params.put("pageSize", pageSize.toString());
        params.put("paperId", context.get("id"));
        String userListData = HttpUtils.GET(ApiUtils.API_URL + "/exam/user/choose/list", params);
        JSONObject jsonObject = JSON.parseObject(userListData);
        JSONArray data = jsonObject.getJSONArray("data");
        int total = jsonObject.getIntValue("total");
        List<Map<String, SimpleStringProperty>> userMapList = new ArrayList<>();
        for (Object datum : data) {
            JSONObject user = (JSONObject) datum;
            Map<String, SimpleStringProperty> userMap = new HashMap<>();

            userMap.put("序号", new SimpleStringProperty(user.getString("id")));
            userMap.put("用户名", new SimpleStringProperty(user.getString("username")));
            userMap.put("手机号码", new SimpleStringProperty(user.getString("phone")));
            userMap.put("电子邮箱", new SimpleStringProperty(user.getString("email")));
            userMap.put("角色", new SimpleStringProperty(user.getString("type")));
            userMap.put("id", new SimpleStringProperty(user.getString("id")));

            userMapList.add(userMap);
        }

        ApiUtils.DataView dataView = new ApiUtils.DataView(Arrays.asList("序号", "用户名", "手机号码", "电子邮箱", "角色"), userMapList).setDataLoad(this).setPageNum(pageNum).setPageSize(pageSize).setTotal(total);
        dataView.setDataLoad(this);

        return dataView;

    }

    public  void commitChoosePaper(Map<String, Object> context, ObservableList<Map<String, SimpleStringProperty>> chooseTableData){

        String id = context.get("id").toString();
        List<String> ids = chooseTableData.stream().map(chooseTableDatum -> chooseTableDatum.get("id").get()).toList();
        Map<String, Object> map = Map.of("id", id, "ids", JSONObject.toJSONString(ids));
        String result = HttpUtils.POST(ApiUtils.API_URL + "/exam/choosePaper/save", map);
        JSONObject jsonObject = JSON.parseObject(result);
        if (jsonObject.getIntValue("code") == 200) {
            AlertUtils.alert_msg("保存成功！");
        } else {
            AlertUtils.alert_warning("保存失败！");
        }

    }
}
